<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- <div v-if="crud.props.searchToggle"> -->
        <!-- 搜索 -->
        <!-- <label class="el-form-item-label">条码</label> -->
        <!-- <el-input v-model="query.barCode" clearable placeholder="条码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <!-- <label class="el-form-item-label">编码</label> -->
        <!-- <el-input v-model="query.model" clearable placeholder="规格" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <!-- <label class="el-form-item-label">产品描述</label> -->
        <!-- <el-input v-model="query.productDesc" clearable placeholder="产品描述" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <!-- <date-range-picker v-model="query.createTime" class="date-item" /> -->
        <!-- <rrOperation :crud="crud" /> -->
      <!-- </div> -->
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <!-- 新增 -->
        <el-button
          slot="left"
          v-permission="['admin','warningMessage:add']"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-upload"
          @click="crud.toAdd"
        >上传
        </el-button>
        <el-button
          slot="right"
          v-permission="['admin','warningMessage:del']"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-upload"
          @click="deleteAll"
        >清空
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.add ? '文件上传' : '编辑文件'" width="500px">
        <el-form ref="form" :model="form" size="small" label-width="80px">
          <el-form-item label="文件名">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <!--   上传文件   -->
          <el-form-item v-if="crud.status.add" label="上传">
            <el-upload
              ref="upload"
              :limit="1"
              :before-upload="beforeUpload"
              :auto-upload="false"
              :headers="headers"
              :on-success="handleSuccess"
              :on-error="handleError"
              :action="baseApi + '/api/warningMessage/upload?name=' + form.name"
            >
              <div class="eladmin-upload"><i class="el-icon-upload" /> 添加文件</div>
              <div slot="tip" class="el-upload__tip">可上传任意格式文件，且不超过100M</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button v-if="crud.status.add" :loading="loading" type="primary" @click="upload">确认</el-button>
          <el-button v-else :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!-- <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="ID">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="条码">
            <el-input v-model="form.barCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="编码">
            <el-input v-model="form.code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品描述">
            <el-input v-model="form.productDesc" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品名称">
            <el-input v-model="form.productName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="规格">
            <el-input v-model="form.model" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位">
            <el-input v-model="form.unit" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="最低库存量">
            <el-input v-model="form.miniStock" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="即时库存量">
            <el-input v-model="form.stock" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品信息">
            <el-input v-model="form.productMessage" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="在途数量">
            <el-input v-model="form.onPassageQuantity" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog> -->
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" :index="indexMethod" />           
        <!-- <el-table-column prop="id" label="ID" /> -->
        <!-- <el-table-column prop="barCode" label="条码" /> -->
        <!-- <el-table-column prop="code" label="编码" /> -->
        <el-table-column prop="productDesc" label="品牌" />
        <!-- <el-table-column prop="productName" label="产品名称" /> -->
        <el-table-column prop="model" label="规格" />
        <el-table-column prop="productMessage" label="建议采购数" />
        <!-- <el-table-column prop="unit" label="单位" /> -->
        <el-table-column prop="stock" label="即时库存量" />
        <el-table-column prop="onPassageQuantity" label="在途数量" />
        <el-table-column prop="miniStock" label="最低库存量" />
        <el-table-column prop="purchaseTime" label="总采购次数" />
        <el-table-column prop="quantity" label="总采购数量" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import crudWarningMessage from '@/api/warningMessage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import dateRangePicker from '@/components/DateRangePicker'

const defaultForm = { id: null, barCode: null, code: null, productDesc: null, productName: null, model: null, unit: null, miniStock: null, stock: null, productMessage: null, onPassageQuantity: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'WarningMessage',
  components: { pagination, crudOperation, rrOperation, dateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '告警信息', url: 'api/warningMessage', idField: 'id', sort: 'id,desc', crudMethod: { ...crudWarningMessage }})
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      permission: {
        add: ['admin', 'warningMessage:add'],
        edit: ['admin', 'warningMessage:edit'],
        del: ['admin', 'warningMessage:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'barCode', display_name: '条码' },
        { key: 'code', display_name: '编码' },
        { key: 'productDesc', display_name: '产品描述' }
      ],
      loading: false
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'fileUploadApi'
    ])
  },
  created() {
    this.crud.optShow = {
      add: false,
      edit: false,
      del: false,
      download: true,
      reset: true
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    indexMethod(index){
      return (index + 1) + (this.crud.page.page -1) * this.crud.page.size
    },
    deleteAll(){
      crudWarningMessage.deleteAll().then(res => {
        this.crud.toQuery()
        this.crud.notify('清空成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    // 上传文件
    upload() {
      this.$refs.upload.submit()
    },
    beforeUpload(file) {
      let isLt2M = true
      isLt2M = file.size / 1024 / 1024 < 100
      if (!isLt2M) {
        this.loading = false
        this.$message.error('上传文件大小不能超过 100MB!')
      }
      this.form.name = file.name
      return isLt2M
    },
    handleSuccess(response, file, fileList) {
      this.crud.notify('上传成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      this.$refs.upload.clearFiles()
      this.crud.status.add = CRUD.STATUS.NORMAL
      this.crud.resetForm()
      this.crud.toQuery()
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 5000
      })
      this.loading = false
    }
  }
}
</script>

<style scoped>

</style>
